'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 数据统计
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var analyze = require('kc-analyze');

var RES_TYPE = {
    DIAMOND: 'diamond',
    GOLD: 'gold',
    FOOD: 'food',
    SOLDIER_SOUL: 'soldier_soul',
    DRAGON_GOLD: 'dragon_gold',
    ENERGY: 'energy',
    SMITHY_ENERGY: 'smithy_energy',
    PAY: 'pay',

    ROLE_CREATE: 'role_create',
    ROLE_LOGIN: 'role_login',
    ROLE_ONLINE_TIME: 'role_online_time',
    ROLE_ONLINE_COUNT: 'role_online_count',
    ROLE_LEVEL: 'role_level',

    // 英雄经验
    // 消耗品
    // 材料(多种)
    // 士兵碎片(多种)
};
/*
 * 后台管理功能 --> 对应的统计数据
 *
 * 用户在线时间(明细) --> 在线统计
 * 用户留在率 --> 创角+登录
 * 在线用户 --> 直接读取游服
 * 用户注册 --> 创角统计
 * 用户登录(总数) --> 登录统计
 * 用户登录IP(明细) --> 读取游服
 * 用户在线分布图 --> 用户在线统计
 */

var EVENT = {
    // 资源
    RES_GET: 200101,
    RES_USE: 200102,
};

/**
 * 统计注册
 * @return {[type]} [description]
 */
exports.recordRegister = function (role_id, user_id, name, channel_id, create_ip) {
    var data = {
        channel_id: channel_id,
        user_id: user_id,
        role_name: name,
        create_ip: create_ip
    };
    analyze.save(role_id, RES_TYPE.ROLE_CREATE, data);
};

/**
 * 统计登陆
 * @return {[type]} [description]
 */
exports.recordLogin = function (role_id, channel_id, create_time) {
    create_time = moment(create_time).unix();
    var data = {
        channel_id: channel_id,
        create_time: create_time
    };
    analyze.save(role_id, RES_TYPE.ROLE_LOGIN, data);
};

/**
 * 统计在线时长
 * @return {[type]} [description]
 */
exports.recordOnlineTime = function (role_id, channel_id, online_time) {
    var login_time = moment().unix() - online_time;
    var data = {
        channel_id: channel_id,
        login_time: login_time,
        online_time: online_time
    };
    analyze.save(role_id, RES_TYPE.ROLE_ONLINE_TIME, data);
};

/**
 * 统计在线用户数
 * @return {[type]} [description]
 */
exports.recordOnlineUserCount = function (channel_id, sys_tm, user_count) {
    var data = {
        channel_id: channel_id,
        sys_tm: sys_tm,
        user_count: user_count
    };
    var role_id = 100;
    analyze.save(role_id, RES_TYPE.ROLE_ONLINE_COUNT, data);
};

/**
 * 同步角色等级
 * @return {[type]} [description]
 */
exports.recordRoleLevel = function (role_id, lv, cb) {
    var data = {
        lv: lv
    };
    analyze.save(role_id, RES_TYPE.ROLE_LEVEL, data, cb);
};

/**
 * 统计物品获得
 * @return {[type]} [description]
 */
exports.recordItemGet = function (role_id, item_type) {
    analyze.record({
        role_id: role_id,
        event_type: EVENT.ITEM_GET,
        item_type: item_type
    });
};

/**
 * 统计物品使用
 * @return {[type]} [description]
 */
exports.recordItemUse = function (role_id, item_type) {
    analyze.record({
        role_id: role_id,
        event_type: EVENT.ITEM_USE,
        item_type: item_type
    });
};

/**
 * 统计金币获得
 * @return {[type]} [description]
 */
/*jshint maxparams: 10 */
exports.recordGoldGet = function (role_id, channel_id, module_type, operation_type, gold_num, reserve_int_val, reserve_str_val) {
    var data = {
        channel_id: channel_id,
        module_type: module_type,
        operation_type: operation_type,
        event_type: EVENT.RES_GET,
        event_value: gold_num,
    };
    if (reserve_int_val !== undefined) {
        data.reserve_int_val = reserve_int_val;
    }
    if (reserve_str_val !== undefined) {
        data.reserve_str_val = reserve_str_val;
    }
    analyze.save(role_id, RES_TYPE.GOLD, data);
};

/**
 * 统计金币使用
 * @return {[type]} [description]
 */
exports.recordGoldUse = function (role_id, channel_id, module_type, operation_type, gold_num, reserve_int_val, reserve_str_val) {
    var data = {
        channel_id: channel_id,
        module_type: module_type,
        operation_type: operation_type,
        event_type: EVENT.RES_USE,
        event_value: gold_num,
    };
    if (reserve_int_val !== undefined) {
        data.reserve_int_val = reserve_int_val;
    }
    if (reserve_str_val !== undefined) {
        data.reserve_str_val = reserve_str_val;
    }
    analyze.save(role_id, RES_TYPE.GOLD, data);
};

/**
 * 统计粮食获得
 * @return {[type]} [description]
 */
/*jshint maxparams: 10 */
exports.recordFoodGet = function (role_id, channel_id, module_type, operation_type, food_num, reserve_int_val, reserve_str_val) {
    var data = {
        channel_id: channel_id,
        module_type: module_type,
        operation_type: operation_type,
        event_type: EVENT.RES_GET,
        event_value: food_num,
    };
    if (reserve_int_val !== undefined) {
        data.reserve_int_val = reserve_int_val;
    }
    if (reserve_str_val !== undefined) {
        data.reserve_str_val = reserve_str_val;
    }
    analyze.save(role_id, RES_TYPE.FOOD, data);
};

/**
 * 统计粮食使用
 * @return {[type]} [description]
 */
exports.recordFoodUse = function (role_id, channel_id, module_type, operation_type, food_num, reserve_int_val, reserve_str_val) {
    var data = {
        channel_id: channel_id,
        module_type: module_type,
        operation_type: operation_type,
        event_type: EVENT.RES_USE,
        event_value: food_num,
    };
    if (reserve_int_val !== undefined) {
        data.reserve_int_val = reserve_int_val;
    }
    if (reserve_str_val !== undefined) {
        data.reserve_str_val = reserve_str_val;
    }
    analyze.save(role_id, RES_TYPE.FOOD, data);
};

/**
 * 统计兵魂获得
 * @return {[type]} [description]
 */
/*jshint maxparams: 10 */
exports.recordSoldierSoulGet = function (role_id, channel_id, module_type, operation_type, soldier_soul_num, reserve_int_val, reserve_str_val) {
    var data = {
        channel_id: channel_id,
        module_type: module_type,
        operation_type: operation_type,
        event_type: EVENT.RES_GET,
        event_value: soldier_soul_num,
    };
    if (reserve_int_val !== undefined) {
        data.reserve_int_val = reserve_int_val;
    }
    if (reserve_str_val !== undefined) {
        data.reserve_str_val = reserve_str_val;
    }
    analyze.save(role_id, RES_TYPE.SOLDIER_SOUL, data);
};

/**
 * 统计兵魂使用
 * @return {[type]} [description]
 */
exports.recordSoldierSoulUse = function (role_id, channel_id, module_type, operation_type, soldier_soul_num, reserve_int_val, reserve_str_val) {
    var data = {
        channel_id: channel_id,
        module_type: module_type,
        operation_type: operation_type,
        event_type: EVENT.RES_USE,
        event_value: soldier_soul_num,
    };
    if (reserve_int_val !== undefined) {
        data.reserve_int_val = reserve_int_val;
    }
    if (reserve_str_val !== undefined) {
        data.reserve_str_val = reserve_str_val;
    }
    analyze.save(role_id, RES_TYPE.SOLDIER_SOUL, data);
};

/**
 * 统计体力获得
 * @return {[type]} [description]
 */
exports.recordEnergyGet = function (role_id, channel_id, module_type, operation_type, energy_num, reserve_int_val, reserve_str_val) {
    var data = {
        channel_id: channel_id,
        module_type: module_type,
        operation_type: operation_type,
        event_type: EVENT.RES_GET,
        event_value: energy_num,
    };
    if (reserve_int_val !== undefined) {
        data.reserve_int_val = reserve_int_val;
    }
    if (reserve_str_val !== undefined) {
        data.reserve_str_val = reserve_str_val;
    }
    analyze.save(role_id, RES_TYPE.ENERGY, data);
};

/**
 * 统计体力使用
 * @return {[type]} [description]
 */
exports.recordEnergyUse = function (role_id, channel_id, module_type, operation_type, energy_num, reserve_int_val, reserve_str_val) {
    var data = {
        channel_id: channel_id,
        module_type: module_type,
        operation_type: operation_type,
        event_type: EVENT.RES_USE,
        event_value: energy_num,
    };
    if (reserve_int_val !== undefined) {
        data.reserve_int_val = reserve_int_val;
    }
    if (reserve_str_val !== undefined) {
        data.reserve_str_val = reserve_str_val;
    }
    analyze.save(role_id, RES_TYPE.ENERGY, data);
};

/**
 * 统计龙币获得
 * @return {[type]} [description]
 */
exports.recordDragonGoldGet = function (role_id, channel_id, module_type, operation_type, dragon_gold_num, reserve_int_val, reserve_str_val) {
    var data = {
        channel_id: channel_id,
        module_type: module_type,
        operation_type: operation_type,
        event_type: EVENT.RES_GET,
        event_value: dragon_gold_num,
    };
    if (reserve_int_val !== undefined) {
        data.reserve_int_val = reserve_int_val;
    }
    if (reserve_str_val !== undefined) {
        data.reserve_str_val = reserve_str_val;
    }
    analyze.save(role_id, RES_TYPE.DRAGON_GOLD, data);
};

/**
 * 统计龙币使用
 * @return {[type]} [description]
 */
exports.recordDragonGoldUse = function (role_id, channel_id, module_type, operation_type, dragon_gold_num, reserve_int_val, reserve_str_val) {
    var data = {
        channel_id: channel_id,
        module_type: module_type,
        operation_type: operation_type,
        event_type: EVENT.RES_USE,
        event_value: dragon_gold_num,
    };
    if (reserve_int_val !== undefined) {
        data.reserve_int_val = reserve_int_val;
    }
    if (reserve_str_val !== undefined) {
        data.reserve_str_val = reserve_str_val;
    }
    analyze.save(role_id, RES_TYPE.DRAGON_GOLD, data);
};

/**
 * 统计精力获得
 * @return {[type]} [description]
 */
exports.recordSmithyEnergyGet = function (role_id, channel_id, module_type, operation_type, smithy_energy_num, reserve_int_val, reserve_str_val) {
    var data = {
        channel_id: channel_id,
        module_type: module_type,
        operation_type: operation_type,
        event_type: EVENT.RES_GET,
        event_value: smithy_energy_num,
    };
    if (reserve_int_val !== undefined) {
        data.reserve_int_val = reserve_int_val;
    }
    if (reserve_str_val !== undefined) {
        data.reserve_str_val = reserve_str_val;
    }
    analyze.save(role_id, RES_TYPE.SMITHY_ENERGY, data);
};

/**
 * 统计精力使用
 * @return {[type]} [description]
 */
exports.recordSmithyEnergyUse = function (role_id, channel_id, module_type, operation_type, smithy_energy_num, reserve_int_val, reserve_str_val) {
    var data = {
        channel_id: channel_id,
        module_type: module_type,
        operation_type: operation_type,
        event_type: EVENT.RES_USE,
        event_value: smithy_energy_num,
    };
    if (reserve_int_val !== undefined) {
        data.reserve_int_val = reserve_int_val;
    }
    if (reserve_str_val !== undefined) {
        data.reserve_str_val = reserve_str_val;
    }
    analyze.save(role_id, RES_TYPE.SMITHY_ENERGY, data);
};

/**
 * 统计钻石获得
 * @return {[type]} [description]
 */
/*jshint maxparams: 10 */
exports.recordDiamondGet = function (role_id, channel_id, module_type, operation_type, diamond_num, reserve_int_val, reserve_str_val) {
    var data = {
        channel_id: channel_id,
        module_type: module_type,
        operation_type: operation_type,
        event_type: EVENT.RES_GET,
        event_value: diamond_num,
    };
    if (reserve_int_val !== undefined) {
        data.reserve_int_val = reserve_int_val;
    }
    if (reserve_str_val !== undefined) {
        data.reserve_str_val = reserve_str_val;
    }
    analyze.save(role_id, RES_TYPE.DIAMOND, data);
};

/**
 * 统计钻石使用
 * @return {[type]} [description]
 */
/*jshint maxparams: 10 */
exports.recordDiamondUse = function (role_id, channel_id, module_type, operation_type, diamond_num, reserve_int_val, reserve_str_val) {
    var data = {
        channel_id: channel_id,
        module_type: module_type,
        operation_type: operation_type,
        event_type: EVENT.RES_USE,
        event_value: diamond_num,
    };
    if (reserve_int_val !== undefined) {
        data.reserve_int_val = reserve_int_val;
    }
    if (reserve_str_val !== undefined) {
        data.reserve_str_val = reserve_str_val;
    }
    analyze.save(role_id, RES_TYPE.DIAMOND, data);
};

/**
 * 统计充值
 * @return {[type]} [description]
 */
exports.recordPay = function (role_id, channel_id, order_info) {
    var data = {
        channel_id: channel_id,
        user_id: order_info.uid,
        name: order_info.name,
        lv: order_info.lv,
        order_id: order_info.order_id,
        game_order: order_info.game_order,
        money: order_info.money
    };

    analyze.save(role_id, RES_TYPE.PAY, data);
};
